package com.github.niefy.config;

import com.github.niefy.modules.third.task.TemplateMsgJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.annotation.PostConstruct;

/**
 * @author : [86188]
 * @description : 定义任务,绑定任务描述与触发器配置
 * @createTime : [2023/3/28 16:52]
 * @updateUser : [86188]
 * @updateTime : [2023/3/28 16:52]
 */

@Configuration
//注册包用于在job中使用spring注入bean
@ComponentScan(basePackages={"cn.hutool.extra.spring"})
@Import(cn.hutool.extra.spring.SpringUtil.class)
public class QuartzConfig {

    @Autowired
    private Scheduler scheduler;

    /**
     * 模板消息拉取定时任务
     * @throws Exception
     */
    @PostConstruct
    protected void InitTemplateMsgJob() throws Exception {
        JobDetail jobDetail = JobBuilder.newJob(TemplateMsgJob.class)
                .withIdentity("TemplateMsgJob")
//                .storeDurably()
                .withDescription("模板消息拉取定时任务")
                .usingJobData("data", "有需要这里可填入信息1")
                .build();
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("TemplateMsgJob")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInMinutes(10)
                .repeatForever())
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
    }
}
